<template>
  <div class="workflow_input">
    <div class="workflow_input_title" v-show="!hideTitle">{{field ? field.alia : ''}}</div>
    <div class="workflow_input_container">
      <a-select class="workflow_select" v-show="!selectData" @change="handleChange" :value="value">
        <a-select-option v-for="item in optionItems" :key="item.value" :value="item.value">
          {{item.label}}
        </a-select-option>
      </a-select>
      <a-select class="workflow_select" v-show="selectData" @change="handleChange">
        <a-select-option v-for="item in optionItems" :key="item.value" :value="item.value">
          {{item.label}}
        </a-select-option>
      </a-select>
      <div class="workflow_input_select" v-if="selectData">{{selectData.alia}}</div>
      <button-popover :output="output" @change="handleSelectChange"></button-popover>
    </div>
  </div>
</template>

<script>
import ButtonPopover from './buttonPopover.vue'
export default {
	props: {
		value: {
			default: ""
		},
    field: Object,
    optionValue:Object,
    hideTitle: Boolean,
		output: {
			default: []
		},
	},
  components:{
    ButtonPopover
  },
	data(){
		return{
      selectData: null
		}
	},
	methods:{
    handleSelectChange(data){
      this.selectData = data
      this.value = ''
			this.$emit('input', `{{${data.parentNodeId}.outputs.${data.parentKey}.${data.name}}}`);
    },
    handleChange(value) {
      this.selectData = null
			this.$emit('input',value);
    },
    findSelectItem(str){
      let split = str.split('.')
      for (let i = 0; i < this.output.length; i++) {
        if(this.output[i].nodeId === split[0]){
          this.selectData = this.output[i].outputs.find(c => c.name === split[split.length - 1])
        }
      }
    }
	},
  mounted(){
    let re = /^{{(\w+(?:\.\w+)+)}}$/
    if(re.test(this.value)){
      this.findSelectItem(re.exec(this.value)[1])
    }
  },
  computed:{
    optionItems(){
      return this.optionValue || this.field?.options?.optionItems || []
    }
  }
}

</script>

<style lang="less">
.workflow_input{
    // display: flex;
    // align-items: center;
    .workflow_input_title{
      margin-bottom: 5px;
    }

    .workflow_input_container{
      display: flex;
      align-items: center;
      position: relative;

      .workflow_select{
        flex: 1 1 auto;
      }

      .workflow_input_select{
        margin: 0 10px;
        padding: 0 10px;
        position: absolute;
        background-color: rgba(127, 255, 212, 0.6);
      }
    }
}
</style>